package com.web.wefun.controller;

import com.web.wefun.entity.User;
import com.web.wefun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //重置密码
    @RequestMapping("/resetPassword")
    @ResponseBody
    public String resetPassword(String password) {
        boolean flag = userService.resetPassword(password);
        if (flag) {
            return "OK";
        }
        return "NOOK";
    }

    //根据用户名查完整用户信息 登录之后操作
    @RequestMapping("/findUserByName")
    @ResponseBody
    public String findByName(String username){
        //调用对应的业务层检查是否存在
        boolean flag = userService.findUserByName(username);
        if (flag) {
            return "OK";
        }
        return "NOOK";
    }

    //根据用户名查手机号码 找回密码用
    @RequestMapping("/findPhoneByName")
    @ResponseBody
    public String findPhoneByName(String username){
        //调用对应的业务层检查是否存在
        boolean flag = userService.findPhoneByName(username);
        if (flag) {
            return "OK";
        }
        return "NOOK";
    }

    //添加用户 进行注册操作 ps:别用NOOK了，总觉得这样写很蠢(不是说你，是说之前这样写的老师)
    @RequestMapping("/register")
    @ResponseBody
    public String addUser(User user){
        if (userService.addUser(user)) {
            return "OK";
        }
        return "NOOK";
    }

    @RequestMapping("/login")
    public String login(String username ,String password, HttpSession session){
        User us = new  User();
        us.setUsername(username);
        us.setPassword(password);
        User user = userService.selectUserByNameAndPassword(us);
        System.out.println(user);
        if (user!=null) {
            session.setAttribute("user",user);
            System.out.println("hello");
            System.out.println(".....");
            return "OK";
        }
        else {
            System.out.println("ERROR");
            return "ERROR";

        }
    }

/*    @RequestMapping("/register")
    public String register(User us, HttpSession session){
        int count = userService.insertUser(us);
        if (count==1)
            return "login";
        else
            return "register";
    }*/

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "index";
    }

/*    @RequestMapping("/checkUserName")
    public void checkUserName(String username, HttpServletResponse resp) throws IOException {
        int msg = 0;
        if(userService.selectUserByName(username)!=null)
            msg = 1;
        resp.getWriter().print(msg);
    }*/


}
